package com.neutech.maplestory.client;

import com.neutech.maplestory.entity.*;
import com.neutech.maplestory.util.MypleStoryFrame;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 游戏的主入口文件
 */
public class MapleStoryClient extends MypleStoryFrame {
    public Hero hero = new Hero(this,300,500);
    public List<Arrow> arrows =new CopyOnWriteArrayList<>();

    public List <Mob> mobs = new CopyOnWriteArrayList<>();

    public List<Item> items = new CopyOnWriteArrayList<>();
    public List<Power> powers = new CopyOnWriteArrayList<>();
    public ItemPackage itemPackage = new ItemPackage(this);

    {
        for (int i = 0; i < 10; i++) {
            Mob mob = new Mob(this,700 + (i * 80),480,5);
            mobs.add(mob);
        }
    }

    /**
     * 方法的重写（重写父类init方法）
     */
    @Override
    public void init() {
        super.init();

        /**
         * 添加键盘监听器
         */
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                hero.keyPressed(e);
                if (e.getKeyCode() == KeyEvent.VK_I){
                    //开关切换
                    itemPackage.live = !itemPackage.live;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
               hero.keyReleased(e);

                }

            });

    }

    /**
     * 方法重写
     * @param g 画笔
     */
    @Override
    public void paint(Graphics g) {
        hero.draw(g);

        //使用迭代器和CopyOnWriteArrayList解决数组索引移动（非线程安全的问题）
        Iterator<Arrow> itArrows = arrows.iterator();
        while (itArrows.hasNext()){
            Arrow arrow = itArrows.next();
            arrow.draw(g);
            arrow.hit(mobs);
        }
        Iterator<Mob> itMobs = mobs.iterator();
        while(itMobs.hasNext()){
            Mob mob = itMobs.next();
            mob.draw(g);
        }
        Iterator<Item> itItems = items.iterator();
        while(itItems.hasNext()){
            Item item = itItems.next();
           item.draw(g);
        }
        Iterator<Power> itPowers = powers.iterator();
        while(itPowers.hasNext()){
            Power power = itPowers.next();
           power.draw(g);
        }


        Font f =g.getFont();
        g.setFont(new Font("微软雅黑",Font.BOLD,30));
        g.drawString("人物方向为：" + hero.dir,100,100);
        g.drawString("人物动作为：" + hero.action,100,150);
        g.drawString("人物动作为：" + hero.down,100,200);
        g.drawString("人物的MP：" + hero.MP,100,250);
        g.drawString("人物的HP：" + hero.HP,100,300);
        g.setFont(f);

        itemPackage.draw(g);
    }

    public static void main(String[] args) {
        //子类调用父类init
        new MapleStoryClient().init();
    }
}
